﻿/* Filename: MD5Hash.cs
 * Author: Vo Manh Cuong
 */

#region --- Used Classes
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;
#endregion 

namespace VMCCryptoLib.HashEncryption
{
   /// <summary>
   /// The MD5 Message-Digest Algorithm is a widely used cryptographic hash function that produces a 128-bit (16-byte) hash value.
   /// Specified in RFC 1321, MD5 has been employed in a wide variety of security applications, and is also commonly used to check data integrity. MD5 was designed by Ron Rivest in 1991 to replace an earlier hash function, MD4.
   /// An MD5 hash is typically expressed as a hexadecimal number, 32 digits long.
   /// However, it has since been shown that MD5 is not collision resistant;[3] as such, MD5 is not suitable for applications like SSL certificates or digital signatures that rely on this property. In 1996, a flaw was found with the design of MD5, and while it was not a clearly fatal weakness, cryptographers began recommending the use of other algorithms, such as SHA-1—which has since been found also to be vulnerable. 
   /// In 2004, more serious flaws were discovered in MD5, making further use of the algorithm for security purposes questionable—specifically, a group of researchers described how to create a pair of files that share the same MD5 checksum.
   /// Further advances were made in breaking MD5 in 2005, 2006, and 2007.
   /// In December 2008, a group of researchers used this technique to fake SSL certificate validity,[7][8] and US-CERT now says that MD5 "should be considered cryptographically broken and unsuitable for further use."[9] and most U.S. government applications now require the SHA-2 family of hash functions.[10]
   /// </summary>
    public class MD5Hash
    {
    	#region --- Public Methods
    	/// <summary>
        /// Method to hash a string with MD5
        /// </summary>
        /// <param name="input">string to hash</param>
        /// <returns>a hashed string</returns>
        public static string ThisString(string input)
        {
            string result = input;
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] hash = md5.ComputeHash(Encoding.ASCII.GetBytes(result));
            StringBuilder stringBuilder = new StringBuilder(); // Use StringBuilder class to improve perf
            foreach (byte b in hash)
            {
                stringBuilder.AppendFormat("0{:x2}", b);
            }
            return result = stringBuilder.ToString();
        }
		
        /// <summary>
        /// Method to hash a file and get a hashed string
        /// </summary>
        /// <param name="path">path of file</param>
        /// <returns>hashed string</returns>
        public static string ThisFile(string path)
        {
            string result = path;
            FileStream streamToRead = new FileStream(result, FileMode.Open, FileAccess.Read); // Read a file
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] hash = md5.ComputeHash(streamToRead);
            streamToRead.Close(); // Clsoe stream
            
            StringBuilder builder = new StringBuilder();
            foreach (byte b in hash)
            {
                builder.AppendFormat("{0:x2}", b);
            }
            return result = builder.ToString();
        }
		#endregion
    }
}
